#include "scene.h"
#include <qDebug>
#include <QMouseEvent>
#include <QEventLoop>
#include <QTimer>

Scene::Scene(QObject *parent) : QGraphicsScene(parent)
{
    ptrCurrentFigure = nullptr;
    ptrCurrentSelection = nullptr;

    figureType = 'e';
    changeFigureType = 'e';
    actionType = 'd';

    centralPoint.setX(0);
    centralPoint.setY(0);
    a = 0;
    b = 0;

    pressed = false;
}

Scene::~Scene()
{

}

void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    pressed = true;

    centralPoint = event->scenePos();
    a = 0;
    b = 0;

    if (actionType == 'd') {
        if (figureType == 'e') {
            triangle.resize(0);
            ptrCurrentFigure = addEllipse(centralPoint.x(),
                                        centralPoint.y(),
                                        1,
                                        1,
                                        QPen(Qt::green, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin),
                                        QBrush(Qt::blue));
        }

        if (figureType == 'r') {
            triangle.resize(0);
            ptrCurrentFigure = addRect(centralPoint.x(),
                                    centralPoint.y(),
                                    1,
                                    1,
                                    QPen(Qt::yellow, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin),
                                    QBrush(Qt::red));
        }

        if (figureType == 't') {
            triangle << QPoint(centralPoint.x(), centralPoint.y());
        }
    }

    if (actionType == 's') {
        selectionArea << event->scenePos();
        selectionArea << QPointF(event->scenePos().x() + 1, event->scenePos().y());
        selectionArea << QPointF(event->scenePos().x() + 1, event->scenePos().y() + 1);
        selectionArea << QPointF(event->scenePos().x(), event->scenePos().y() + 1);

        qDebug() << "PRESS" << selectionArea.size();
        ptrCurrentSelection = addPolygon(selectionArea,
                                QPen(Qt::yellow, 1, Qt::DashLine, Qt::SquareCap, Qt::MiterJoin),
                                QBrush(Qt::NoBrush));
    }

    event->accept();
}

void Scene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (actionType == 'd') {
        a = abs(centralPoint.x() - event->scenePos().x());
        b = abs(centralPoint.y() - event->scenePos().y());

        if (figureType == 'e') {
            (static_cast<QGraphicsEllipseItem*>(ptrCurrentFigure))->setRect(centralPoint.x() - a,
                            centralPoint.y() - b,
                            2 * a,
                            2 * b);
        }

        if (figureType == 'r') {
            (static_cast<QGraphicsRectItem*>(ptrCurrentFigure))->setRect(centralPoint.x() - a,
                            centralPoint.y() - b,
                            2 * a,
                            2 * b);
        }
    }

    if (actionType == 's' && pressed){
        QPointF crossPoint(selectionArea[0].x(), event->scenePos().y());
        selectionArea[3] = crossPoint;
        crossPoint.setX(event->scenePos().x());
        crossPoint.setY(selectionArea[0].y());
        selectionArea[1] = crossPoint;
        selectionArea[2] = event->scenePos();
        qDebug() << "MOVE" << selectionArea.size();

        (static_cast<QGraphicsPolygonItem*>(ptrCurrentSelection))->setPolygon(selectionArea);
    }

    event->accept();
}

void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if (actionType == 'd') {
        vector<double> data;

        if (figureType == 't') {
            qDebug() << triangle.size();
            for (int i = 0; i < triangle.size(); ++i) {
                qDebug() << triangle[i];
            }
            if (triangle.size() == 3) {
                ptrCurrentFigure = addPolygon(triangle,
                                              QPen(Qt::magenta, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin),
                                              QBrush(Qt::white));
                for (size_t i = 0; i < 3; ++i) {
                    data.push_back(triangle[i].x());
                    data.push_back(triangle[i].y());
                }
                data.push_back(0);

                triangle.resize(0);

                ptrCurrentFigure->setFlag(QGraphicsItem::ItemIsSelectable);
                emit signalGetNewFigure(data, ptrCurrentFigure);
            }
        } else if (a != 0 && b != 0) {
            data.push_back(centralPoint.x() - a);
            data.push_back(centralPoint.y() - b);
            data.push_back(2 * a);
            data.push_back(2 * b);
            data.push_back(0);

            ptrCurrentFigure->setFlag(QGraphicsItem::ItemIsSelectable);
            emit signalGetNewFigure(data, ptrCurrentFigure);
        }
    }
    if (actionType == 's' && selectionArea.size() != 0) {
        qDebug() << selectionArea[0] << selectionArea[1] << selectionArea[2] << selectionArea[3];
        qDebug() << "REALISE" << selectionArea.size();
        QPainterPath path;
        path.addPolygon(selectionArea);
        this->setSelectionArea(path);

        emit signalGetSelectionFigures(this->items(selectionArea));
        qDebug() << this->items(selectionArea).empty();
        removeItem(reinterpret_cast<QGraphicsItem*>(ptrCurrentSelection));
        selectionArea.resize(0);
    }

    if (actionType == 'c') {
        a = event->scenePos().x() - centralPoint.x();
        b = event->scenePos().y() - centralPoint.y();
        emit signalGetCoef(pair<double, double>(a, b));
    }

    pressed = false;
    event->accept();
}

void Scene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    qDebug() << "DOUBLE";
    event->accept();
}

void Scene::slotEllipseState()
{
    figureType = 'e';
}

void Scene::slotRectangleState()
{
    figureType = 'r';
}

void Scene::slotTriangleState()
{
    figureType = 't';
}

void Scene::slotExtension()
{
    changeFigureType = 'e';
}

void Scene::slotShift()
{
    changeFigureType = 's';
}

void Scene::slotTurn()
{
    changeFigureType = 't';
}
